<template>
  <div>
    <Button type="success" :to="{path:'materialType-add',query:{}}">
      添加物料类型
    </Button>
    <Card style="width:100%">
      <row type="flex" justify="space-between">
        <i-col>

          <span class="key" style="margin-left: 20px">物料类型:</span>
          <Input v-model="typeName" placeholder="请输入物料类型" class="width-condition" style="width: 180px"/>

          <Button type="success" icon="ios-search" style="margin-left: 50px" @click="find">查询</Button>
        </i-col>
      </row>
    </Card>

    <Table stripe :columns="columns" :data="data"></Table>

    <row style="margin-top: 10px">
      <i-col align="right">
        <Page show-sizer :total="total" show-total @on-change="onChange" @on-page-size-change="onPageSizeChange"/>
      </i-col>
    </row>

  </div>
</template>

<script>
  import { MaterialTypeFindAll,MaterialTypeDelete} from "../../../api/data"

  export default {
    data() {
      return {
        page: 1,//page 第几页
        pageSize: 10,//pageSize:每页几条数据
        total: 0,
        typeName: '',
        columns: [
          {
            title: '物料类型名称',
            key: 'typeName'
          },
          {
            title: '类型',
            key: 'type'
          },
          {
            title: '描述',
            key: 'describe'
          },
          {
            title: '操作',
            key: 'action',
            width: 180,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.$router.push({
                        path: 'materialType-update', query: {
                          id: params.row.id,


                        }
                      })
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '确认删除',
                        content: '是否要删除物料类型？',
                        onOk: () => {
                          this.MaterialTypeDelete(params.row.id);
                        },
                        onCancel: () => {
                        }
                      })
                    },
                  }
                }, '删除')
              ]);
            }
          }

        ],
        materialClassificationList:[],
        data: []
      }
    },
    mounted() {
      this.MaterialTypeFindAll();
    },
    methods: {
      find() {
        this.MaterialTypeFindAll();
      },
      MaterialTypeFindAll() {
        var data = {
          page: this.page,
          pageSize: this.pageSize,
          typeName: this.typeName,
        };
        MaterialTypeFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.data = data.data.data.list;
            this.total = data.data.data.total;
          }
        })
      },
      onPageSizeChange(pageSize) {
        this.page = 1;
        this.pageSize = pageSize;
        this.MaterialTypeFindAll();
      },
      onChange(page) {
        this.page = page;
        this.MaterialTypeFindAll();
      },
      MaterialTypeDelete(id) {
        let data = {
          id: id
        }
        MaterialTypeDelete(data).then(res => {
          if ( res.data.code==0){
            this.$Message.success("删除成功");
            this.MaterialTypeFindAll();
          } else {
            this.$Message.error(res.data.msg);
          }
        })
      },
    }
  }
</script>

<style>
  .width-condition {
    width: 150px;
    margin-left: 10px;
    margin-right: 10px;
  }
</style>
